package com.ysm.smfu.chat;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.bean.Msg;
import com.ysm.smfu.bean.SysMsg;
import com.ysm.smfu.utils.ThreadUtils;
import com.ysm.smfu.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import chatuidemo.db.DBManager;

/**
 * Created by Administrator on 2016/10/30.
 */
public class SysMsgActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.list)
    ListView list;
    private SysMsg sysMsg;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_sysmsg);
        ButterKnife.bind(this);
    }

    List<Msg> no = new ArrayList<>();
    List<Msg> has = new ArrayList<>();

    @Override
    protected void initData() {
        super.initData();
        titleBody.setText("系统消息");
        mainActivityTopbarImg.setVisibility(View.GONE);
        ThreadUtils.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                sysMsg = DBManager.getInstance().getSysMsg();
                List<Msg> message = sysMsg.message;
                for (Msg msg : message) {
                    if (TextUtils.equals(msg.messageState, "1")) {
                        has.add(msg);
                    } else {
                        no.add(msg);
                    }
                }
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list.setAdapter(new Myadapter());
                    }
                });
            }
        });
    }


    class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return sysMsg.message.size();
        }

        @Override
        public Object getItem(int position) {
            return sysMsg.message.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = UIUtils.inflate(R.layout.item_sys);
                viewHolder = new ViewHolder(convertView);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Msg msg;
            if (position < no.size()) {
                msg = no.get(position);
                viewHolder.send_time.setTextColor(getResources().getColor(R.color.black));
                viewHolder.sys_content.setTextColor(getResources().getColor(R.color.black));
                viewHolder.read_time.setTextColor(getResources().getColor(R.color.black));
            } else {
                msg = has.get(position - no.size());
                viewHolder.send_time.setTextColor(getResources().getColor(R.color.gray));
                viewHolder.sys_content.setTextColor(getResources().getColor(R.color.gray));
                viewHolder.read_time.setTextColor(getResources().getColor(R.color.gray));
            }
            viewHolder.send_time.setText(msg.message_time);
            viewHolder.sys_content.setText(msg.syMessage);
            viewHolder.read_time.setText(msg.read_time);
            return convertView;
        }
    }

    class ViewHolder {
        public TextView send_time;
        public TextView sys_content;
        public TextView read_time;

        public ViewHolder(View view) {
            send_time = (TextView) view.findViewById(R.id.send_time);
            sys_content = (TextView) view.findViewById(R.id.sys_content);
            read_time = (TextView) view.findViewById(R.id.read_time);
            view.setTag(this);
        }
    }
}
